﻿using AutoMapper;
using LianChuDemo.Bll.BaseInfoManagerBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WenDangOne.Common.Lib.TypeTools;
using WenDangOne.DbModel.BaseInfoManagerDal;
using WenDangOne.Lib;
using WenDangOne.ViewModel.BaseInfoManagerView;
using WenDangOne.ViewModel.common;

namespace WenDangOne.Controllers.BaseInfoManager
{
    //进行重构
    [ApiModuleRelation("diqige")]
    public class ModuleManagerController : MyBaseController
    {
        //注入
        private ModuleDbBll _moduleDbBll;
        private IMapper _mapper;
        //写构造函数
        public ModuleManagerController(ModuleDbBll moduleDbBll,IMapper mapper)
        {
            _moduleDbBll = moduleDbBll;
            _mapper = mapper;
        }
        [HttpGet]
        public ResultModel<List<ModuleDb>> GetModuleDbsAll()
        {
            try
            {
                var data = _moduleDbBll.GetModuleDbsAll();
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<List<ModuleDb>>(ex);
            }
        }
        [HttpGet]
        public ResultModel<List<ModuleDbView>> GetModuleDbViewsAll()
        {
            try
            {
                var data = _moduleDbBll.GetModuleDbViewsAll();
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<List<ModuleDbView>>(ex);
            }
        }
        [HttpGet]
        public ResultModel<bool> VModuleCode(string mc)
        {
            try
            {
                var data = _moduleDbBll.VModuleCode(mc);
                return MyOk<bool>(data);
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }

        [HttpPost]
        public ResultModel<bool> ModuleAdd(ModuleDb view)
        {
            if(string.IsNullOrWhiteSpace(view.module_code))
            {
                return MyFail<bool>("请输入模块code");
            }
            if (!_moduleDbBll.VModuleCode(view.module_code))
            {
                return MyFail<bool>("模块code已经被使用,请重新输入其他模块Code");
            }
            ModuleDb dbModel = ModelCopier.CreateInstanceFrom<ModuleDb>(view);
            //这是添加的时候把姓名和时间进行一个赋值
            dbModel.creator_date = DateTime.Now;
            dbModel.creator_name = LoginUser.user_name;
            try
            {
                var data = _moduleDbBll.ModuleAdd(dbModel);
                return MyOk<bool>(data);
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }

        //反填
        [HttpGet]
        public ResultModel<ModuleDbView> GetMViewByCode(string code)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                return MyFail<ModuleDbView>("参数错误, 缺少code参数");
            }
            try
            {
                var data = _moduleDbBll.GetModuleDbViewByCode(code);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<ModuleDbView>(ex);
            }
        }
        [HttpGet]
        public ResultModel<ModuleDbView> GetModuleDbViewByCodeSonSql(string moduleCode)
        {
            try
            {
                var data = _moduleDbBll.GetModuleDbViewByCodeSonSql(moduleCode);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<ModuleDbView>(ex);
            }
        }

        [HttpPost]
        public ResultModel<bool> ModuleUpdate(ModuleDbView view)
        {
            if (string.IsNullOrWhiteSpace(view.module_code))
            {
                return MyFail<bool>("参数错误, 缺少module_code参数");
            }
            ModuleDb dbModel = _mapper.Map<ModuleDb>(view);
            dbModel.modifi_date = DateTime.Now;
            dbModel.modifi_name = "lzh";
            try
            {
                var data = _moduleDbBll.ModuleUpdate(dbModel);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }
    }
}
